﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.TestManagement.Client;

namespace TFSTools.TFSLibrary.TestManagement
{
    public abstract class TFSTestAction : TFSServerEntity
    {
        private TFSTestCase tfsTestCase;

        internal TFSTestAction(TFSTestCase testCase, ITestAction testAction, decimal stepNumber)
            : base(testCase.Connection)
        {
            this.ServerTestAction = testAction;
            this.StepNumber = stepNumber;
            this.tfsTestCase = testCase;

            this.ParameterDelimiter = '\x01';
        }

        protected ITestAction ServerTestAction { get; set; }

        public char ParameterDelimiter { get; private set; }

        public decimal StepNumber { get; private set; }

        public abstract string Title { get; }

        public virtual string ExpectedResult
        {
            get { return string.Empty; }
        }

        public abstract TFSTestActionType ActionType { get; }

        public string Comment { get; set; }

        public TFSTestCase TestCase { get { return this.tfsTestCase; } }
    }

    public sealed class TFSTestActionCollection : TFSServerEntityCollection<TFSTestAction>
    {
        public TFSTestActionCollection(ITFSConnection connection)
            : base(connection)
        {
        }

        public TFSTestActionCollection(ITFSConnection connection, IList<TFSTestAction> list)
            : base(connection, list)
        {
        }

        public TFSTestActionCollection(ITFSConnection connection, int capacity)
            : base(connection, capacity)
        {
        }

        public TFSTestAction FindStep(decimal stepNumber)
        {
            return this.FirstOrDefault(x => x.StepNumber == stepNumber);
        }

        internal static TFSTestActionCollection Create(TFSTestCase testCase)
        {
            TestActionBuilder testActions = new TestActionBuilder();

            PopulateTestActions(testCase, testActions, testCase.ServerTestCase.Actions);

            return new TFSTestActionCollection(testCase.Connection, testActions);
        }

        private static void PopulateTestActions(TFSTestCase testCase, TestActionBuilder testActionBuilder, TestActionCollection testActions)
        {
            foreach (ITestAction action in testActions)
            {
                if (action is ITestStep)
                {
                    testActionBuilder.Add(new TFSTestStep(testCase, (ITestStep)action, testActionBuilder.StepNumber, testActionBuilder.FromSharedStep));
                }
                else if (action is ISharedStepReference)
                {
                    var sharedStepReference = new TFSSharedStepReference(testCase, (ISharedStepReference)action, testActionBuilder.StepNumber);
                    testActionBuilder.Add(sharedStepReference);
                    var sharedStepActions = sharedStepReference.SharedStep.Actions;

                    decimal startStepNumber = testActionBuilder.StepNumber;
                    testActionBuilder.StepNumber += 0.1M;
                    testActionBuilder.FromSharedStep = true;

                    PopulateTestActions(testCase, testActionBuilder, sharedStepActions);

                    testActionBuilder.StepNumber = startStepNumber;
                    testActionBuilder.FromSharedStep = false;
                }
                else
                {
                    testActionBuilder.Add(new TFSUnknownAction(testCase, action, testActionBuilder.StepNumber));
                }

                if (testActionBuilder.FromSharedStep)
                {
                    testActionBuilder.StepNumber += 0.1M;
                }
                else
                {
                    testActionBuilder.StepNumber += 1M;
                }
            }
        }

        private class TestActionBuilder : List<TFSTestAction>
        {
            public TestActionBuilder()
            {
                this.StepNumber = 1;
                this.FromSharedStep = false;
            }

            public decimal StepNumber { get; set; }

            public bool FromSharedStep { get; set; }
        }
    }
}
